package cn.itcast.core.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.itcast.common.utils.Page;
import cn.itcast.core.bean.BaseDict;
import cn.itcast.core.bean.Customer;
import cn.itcast.core.bean.QueryVo;
import cn.itcast.core.service.CustomerService;

@Controller
public class CustomerController {

	@Autowired
	private CustomerService customerService;
	
	@RequestMapping(value = "customer.action")
	public String customer(Model model,QueryVo queryVo){
		// 	客户来源  
		List<BaseDict> fromType = customerService.findBaseDictByTypeCode("002");
		model.addAttribute("fromType", fromType);
		// 客户行业  001
		List<BaseDict> industryType = customerService.findBaseDictByTypeCode("001");
		model.addAttribute("industryType", industryType);
		
		// 客户级别  006
		List<BaseDict> levelType = customerService.findBaseDictByTypeCode("006");
		model.addAttribute("levelType", levelType);
		
		// 组装客户数据
		Page<Customer> page = customerService.findCustomerByQueryVo(queryVo);
		model.addAttribute("page", page);
		
		// 条件回显
		model.addAttribute("custName", queryVo.getCustName());
		model.addAttribute("custSource", queryVo.getCustSource());
		model.addAttribute("custIndustry", queryVo.getCustIndustry());
		model.addAttribute("custLevel", queryVo.getCustLevel());
		
		return "customer";
	}
	
	// 修改第一步  根据id获取信息
	@RequestMapping(value = "customer/edit.action")
	@ResponseBody
	public Customer edit(Long id){
		Customer customer = customerService.findCustomerById(id);
		return customer;
	}
	
	// 修改第二步  修改信息
	@RequestMapping(value = "customer/update.action")
	@ResponseBody
	public String update(Customer customer){
		customerService.updateCustomer(customer);
		return "ok";
	}
	
	// 删除
	@RequestMapping(value = "customer/delete.action")
	@ResponseBody
	public String delete(Long id){
		customerService.deleteCustomerById(id);
		return "ok";
	}
}
